package global.com.loansapplication.ui.actviity.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.superrecycleview.superlibrary.recycleview.ProgressStyle;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.Bind;
import global.com.loansapplication.R;
import global.com.loansapplication.base.BaseActivity;
import global.com.loansapplication.bean.ClueInfo;
import global.com.loansapplication.event.NetErrorEvent;
import global.com.loansapplication.event.RechargeEvent;
import global.com.loansapplication.ui.actviity.user.biz.OwnerBiz;
import global.com.loansapplication.ui.actviity.user.component.DaggerOwnerComponent;
import global.com.loansapplication.ui.actviity.user.contract.OwnerContract;
import global.com.loansapplication.ui.actviity.user.module.OwnerModule;
import global.com.loansapplication.ui.actviity.user.presenter.OwnerPresenter;
import global.com.loansapplication.ui.adapter.OwnAdapter;
import rx.functions.Action1;

public class OwnerAct extends BaseActivity implements OwnerContract.View, SuperRecyclerView.LoadingListener {

    @Inject
    OwnerPresenter mPresenter;
    @Inject
    OwnerBiz mBiz;

    private OwnAdapter mAdapter;

    @Bind(R.id.needsx)
    TextView needsx;
    @Bind(R.id.image_no_data)
    ImageView mImageNoData;

    @Bind(R.id.super_recycler_view)
    SuperRecyclerView superRecyclerView;

    @Override
    protected void setRoot() {
        setTitleBar("#FFFFFFFF");
        setContentView(R.layout.activity_owner);
    }

    @Override
    protected void setup() {
        setStateText();
        setToolBar("我抢的", "");
        DaggerOwnerComponent.builder().ownerModule(new OwnerModule(this)).build().inject(this);
        needsx.setText("导出");
        mPresenter.setBiz(mBiz);
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        superRecyclerView.setLayoutManager(layoutManager);
        superRecyclerView.setRefreshEnabled(true);// 可以定制是否开启下拉刷新
        superRecyclerView.setLoadMoreEnabled(true);// 可以定制是否开启加载更多
        superRecyclerView.setLoadingListener(this);// 下拉刷新，上拉加载的监听
        superRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);// 下拉刷新的样式
        superRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);// 上拉加载的样式
        superRecyclerView.setArrowImageView(R.mipmap.iconfont_downgrey);// 设置下拉箭头

        mPresenter.getList();

        RxView.clicks(needsx)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        mPresenter.toExportData();
                    }
                });
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBiz.onDestory();
    }

    @Override
    public void onRefresh() {
        mPresenter.refresh();
        mPresenter.getList();
    }

    @Override
    public void onLoadMore() {
        mPresenter.getList();
    }

    @Override
    public void updateCollection(List<ClueInfo> coupons) {
        if (mAdapter == null) {
            mAdapter = new OwnAdapter(this, coupons);
            superRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void refreshComplete() {
        superRecyclerView.completeRefresh();
    }

    @Override
    public void loadMoreComplete() {
        superRecyclerView.completeLoadMore();
    }

    @Override
    public void hasLoadMore(boolean b) {
        superRecyclerView.setNoMore(b);
    }

    @Override
    public void setNoDate(boolean b) {
        if (b) {
            mImageNoData.setVisibility(View.VISIBLE);
        } else {
            mImageNoData.setVisibility(View.GONE);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSetEvent(RechargeEvent info) {
        mPresenter.refresh();
        mPresenter.getList();
    }
}
